package org.scboy.rbac.service.imp;

import java.util.Date;
import java.util.List;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.rbac.dao.RbacResourceOrgTypeDao;
import org.scboy.rbac.service.ResourceOrgTypeService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.lang.UUID;


@Service("rbac_roleOrgTypeServiceImp")
public class ResourceOrgTypeServiceImp implements ResourceOrgTypeService {


	@Autowired
	EntityFactory factory;
	
	@Override
	@Transactional
	public boolean addResource(String orgTypeId, String[] resId) {
		RbacResourceOrgTypeDao dao=factory.getDao(RbacResourceOrgTypeDao.class);
		//删除掉原有的关联
		dao.delete(Model.create().add("orgTypeId", orgTypeId));
		//增加新的关联
		if(resId!=null){
			for (String rid : resId) {
				Model roleUserType=new Model();
				roleUserType.add("id", UUID.randomUUID().toString());
				roleUserType.add("resId", rid);
				roleUserType.add("orgTypeId", orgTypeId);
				roleUserType.add("addTime", new Date());
				roleUserType.add("editTime", new Date());
				dao.insert(roleUserType);
			}
		}
		return true;
	}

	@Override
	public boolean delete(String id) {
		RbacResourceOrgTypeDao dao=factory.getDao(RbacResourceOrgTypeDao.class);
		int result=dao.delete(Model.create().add("id", id));
		if(result>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Page page(Model params) {
		RbacResourceOrgTypeDao dao=factory.getDao(RbacResourceOrgTypeDao.class);
		return dao.queryPage(params);
	}

	@Override
	public String[] getResIds(String orgTypeId) {
		RbacResourceOrgTypeDao dao=factory.getDao(RbacResourceOrgTypeDao.class);
		List<Model> result=dao.select(Model.create().add("orgTypeId", orgTypeId));
		String [] resIds=new String [result.size()];
		for (int i = 0; i < result.size(); i++) {
			resIds[i]=result.get(i).getString("resId");
		}
		return resIds;
	}
	
}
